<?php
if(!defined('_SetYour_')) die('Error');# Definicion de seguridad

class Jerarquia extends Class_Jerarquia{
	function Deflate_Tree ($matriz,$nivel,$estilo){	#Tercer argumento para definir un doble estilo a la clase
		foreach($matriz as $id=>$value){
/* ================ Edicion del formato HTML ================ */
if($estilo=='tree'){
echo "<tr style='background:#".dechex(13434624-($nivel*13056))."'>
<td onmousemove=\"document.getElementById('mnu_pagina_".$id."').style.visibility='visible';\" onmouseout=\"document.getElementById('mnu_pagina_".$id."').style.visibility='hidden';\">";
if($GLOBALS['U']['pag_org'])echo "<input type='radio' name='lst_pagina' value='".$id.":".$value[2]."'/>";
echo str_repeat("-- ",$nivel)." ".htmlentities($value[3])."<div id='mnu_pagina_".$id."' class='pag_opt'>";
if($GLOBALS['U']['pag_edit'])echo "<a href='scripts.php?s=editor&id=".$id."' class='link-blue'>Ver y editar</a>";
if($GLOBALS['U']['pag_org'])echo " | <a href='admin-wmb.php?modulo=paginas&id=".$id."&borrar' onclick=\"if(confirm('Confirma borrar esta pagina?')) document.location.replace('admin-wmb.php?modulo=paginas&id=".$id."&borrar'); return false;\" class='link-red'>Borrar</a>";
echo"</div></td>
<td>".((!empty($value[7]))?htmlentities($value[7]):"<u>Sin autor</u>")."</td>
<td title='".date('d-m-Y h:i:s',strtotime($value[5]))."' style='background:#".(($value[6])?"009966":"FFCC33")."'><label>".date('d-m-Y',strtotime($value[5]))."</label>
<br />".(($value[6])?"Publicado":"No Publicado")."</td>
</tr>";
}elseif($estilo=='select'){
echo "<option value='".$id.":".$value[2]."'>".str_repeat("-- ",$nivel)." ".htmlentities($value[3])."</option>";
}
/* FIN */
			if(isset($value['hijo'])){
				$this->Deflate_Tree($value['hijo'],$nivel+1,$estilo);
			}
		}
	}
	function mostrar($estilo){
		$this->Deflate_Tree($this->arbol,0,$estilo);
	}
	
}

class Class_Jerarquia {
	var $matriz;
	var $arbol;
	var $ORG;
	function Class_Jerarquia (){ # Constructor para futuras funciones
	}
	# 1 arg ID, 2 arg nivel, 3 arg parent
	function insertar($id){
		$insercion = func_get_args();
		foreach($insercion as $Id => $value){# recorre para acomodar indices
			$this->matriz[$id][]=$value;
		}
	}
	function Ordenar_Nivel($id){
		ksort($this->matriz);# Ordenar por clave
		$nivel=1;
	$this->arbol=$this->matriz;
############ NUEVA MODIFICACION PARA INDENTIFICAR PARENT
while($this->arbol){
	foreach($this->arbol as $key => $value){
		if($value[2]==0){
			$this->ORG[$key]['N']=1;
			unset($this->arbol[$key]);
			continue;# Obtiene si es Fila 0
		}	
		$sta=$this->ORG[$this->arbol[$key][2]]['N'];
		if($sta){
				if(!isset($this->ORG[$key]['A'])) $this->ORG[$key]['A']=$value[2];
				$this->ORG[$key]['A'].=":".$this->ORG[$value[2]]['A'];
			$this->ORG[$key]['N']=$sta+1;
			unset($this->arbol[$key]);
		}
	}
	
}
##############
		foreach($this->ORG as $key => $value){
		$this->matriz[$key][1]=$value['N'];
		}
#####<<<<<
return ":".$this->ORG[intval($id,10)]['A']; # Casting
unset($this->ORG);
}
function Ordenar_Arbol(){
		$clean=false;
		 $nivelmax=(int)NULL;
		foreach($this->matriz as $Id => $value){# examina matriz
			if(!$clean){$this->matriz=array();$clean=true;}# Limpia la matriz preparandola para la redefinicion
			$nivel=$value[1];# obtine nivel de organizacion actual
			$this->matriz[$nivel][$Id]=$value;# REDEFINIENDO LA MATRIZ
			if($nivelmax<$nivel) $nivelmax=$nivel;//obtiene el maximo nivel de organizacion
		}
		while($nivelmax>1){
			foreach($this->matriz[$nivelmax] as $key => $value){
				$this->matriz[$nivelmax-1][$value[2]]['hijo'][$key]=$value;
			}
			$nivelmax--;
		}
		$this->arbol=$this->matriz[1];
	}
}
?>